home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_01_04 / 1n04017a < prev    next >
Text File  |  1990-07-17  |  3KB  |  75 lines

  1. /*
  2. **  Figure 2 - Our own non-compiler specific find first/next calls
  3. */
  4.  
  5. #include        <stdio.h>
  6. #include        <stdlib.h>
  7. #include        <dos.h>
  8. #include        "dirent.h"
  9.  
  10. /************************************************************************/
  11. /*                                                                      */
  12. /* rfind_1st() - Find first matching file                               */
  13. /*                                                                      */
  14. /* Parameters: 1 - Drive, path and filename of file to be found. May    */
  15. /*                 include wildcards                                    */
  16. /*             2 - Attribute of file to search for. Attributes are      */
  17. /*                 described in the MS-DOS manual. The search strategy  */
  18. /*                 is described under DOS call 0x4Eh.                   */
  19. /*             3 - Disk transfer area buffer. If NULL, one will be      */
  20. /*                 malloc'ed.                                           */
  21. /* Returns: Pointer to a struct DSTRUCT. If error, NULL is returned and */
  22. /*          _doserrno is set to the error #.                            */
  23. /*                                                                      */
  24. /************************************************************************/
  25.  
  26. struct DSTRUCT *rfind_1st(char *name, unsigned attribute, struct DSTRUCT *dta)
  27. {
  28.         struct DSTRUCT *my_dta;
  29.         union REGS regs;
  30.  
  31.         if (NULL == dta)
  32.                 my_dta = (struct DSTRUCT *)malloc((int)sizeof(struct DSTRUCT));
  33.         else    my_dta = dta;
  34.  
  35.         bdos(0x1A, (unsigned)my_dta, 0);/* set DTA to my_dta            */
  36.         regs.x.ax = 0x4E00;             /* find first                   */
  37.         regs.x.dx = (unsigned)name;
  38.         regs.x.cx = attribute;
  39.         intdos(®s, ®s);
  40.         if (regs.x.cflag)               /* if error                     */
  41.         {
  42.                 _doserrno = regs.x.ax;
  43.                 if (NULL == dta && my_dta != NULL)
  44.                         free(my_dta);
  45.                 return (struct DSTRUCT *) NULL;
  46.         }
  47.         return my_dta;
  48. }
  49.  
  50. /************************************************************************/
  51. /*                                                                      */
  52. /* rfind_nxt() - Find next matching file                                */
  53. /*                                                                      */
  54. /* Parameters: 1 - Pointer to DSTRUCT structure to use                  */
  55. /*                                                                      */
  56. /* Returns: Pointer to struct DSTRUCT,                                  */
  57. /*          NULL if no more matching files found                        */
  58. /*                                                                      */
  59. /************************************************************************/
  60.  
  61. struct DSTRUCT *rfind_nxt(struct DSTRUCT *dta)
  62. {
  63.         union REGS regs;
  64.  
  65.         bdos(0x1A, (unsigned)dta, 0);   /* set DTA to dta               */
  66.         regs.x.ax = 0x4F00;
  67.         intdos(®s,®s);
  68.         if (regs.x.cflag)               /* if error                     */
  69.         {
  70.                 _doserrno = regs.x.ax;
  71.                 return (struct DSTRUCT *) NULL;
  72.         }
  73.         return dta;
  74. }
  75.